﻿using System;
using System.ComponentModel.Composition;
using System.Threading;
using MediaAssistant.Data;
using MefBasic.Threading;

namespace MediaAssistant.Management
{
    public abstract class BackgroundScanner
    {
        protected Job Job { get; set; }

        private void CreateJob()
        {
            Job = new Job(StartScanner);
        }
        [Import]
        protected Lazy<LibraryDataSource> DataSource { get; set; }
        [Import]
        protected StatusMessageService StatusMessageService { get; set; }
 
        protected string Name { get; set; }
        protected bool CancelPendingJobRequested { get; set; }

        public void Start()
        {
            CreateJob();
            Job.Start();
        }

        public virtual void Stop()
        {
            if (Job==null)
                return;
            Job.Stop();
            Job = null;
        }

        protected void SetStatusMessage(string message)
        {
            //Note: Because of status message Library scrolled to top. I don't know why.
            //return;
            StatusMessageService.SetStatusMessage(message);
        }

        public void Pause()
        {
            if (Job == null) 
                return;
            SetStatusMessage(string.Format("Pausing {0}", Name));
            Job.Pause();
            SetStatusMessage(string.Format("{0} Paused", Name));
        }

        public void Resume()
        {
            if(Job ==null)
                return;
            Job.Resume();
            SetStatusMessage(string.Format("{0} Resumed",Name));
        }

        protected void StartScanner(Job obj)
        {
            while (obj.Store.StopRequested == false)
            {
                if (obj.Store.StopRequested)
                {
                    return;
                }
                Thread.Sleep(1000);
                PauseIfRequested();
                try
                {
                    TryToScan();
                }
                catch (Exception)
                {
                    
                }
            }
        }

        protected abstract void TryToScan();

        protected virtual bool ShallReturn()
        {
            bool shallReturn = false;
            if (Job.Store.StopRequested)
            {
                shallReturn = true;
            }
            Thread.Sleep(100);
            return shallReturn;
        }

        protected virtual bool PauseIfRequested()
        {
            if (Job.Store.PauseRequested)
            {
                Job.Store.Status = JobStatus.Paused;
                while (Job.Store.PauseRequested)
                {
                    if(ShallReturn())
                        return true;
                    Thread.Sleep(100);
                }
            }
            return false;
        }

        protected void CancelPendingJob()
        {
            CancelPendingJobRequested = true;
            while (CancelPendingJobRequested)
            {
                Thread.Sleep(100);
            }
        }
    }
}